@model EIMS.Models.ViewModels.CustomerDetailViewModel

@{
    ViewData["Title"] = "客户详情";
}

<div class="container-fluid px-4">
    <h1 class="mt-4">@ViewData["Title"]</h1>
    <ol class="breadcrumb mb-4">
        <li class="breadcrumb-item"><a asp-controller="Home" asp-action="Index">首页</a></li>
        <li class="breadcrumb-item"><a asp-controller="Customer" asp-action="Index">客户管理</a></li>
        <li class="breadcrumb-item active">客户详情</li>
    </ol>

    <div class="card mb-4">
        <div class="card-header">
            <i class="fas fa-user-tie me-1"></i>
            客户基本信息
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-6">
                    <dl class="row">
                        <dt class="col-sm-4">
                            @Html.DisplayNameFor(model => model.Customer.CustomerNumber)
                        </dt>
                        <dd class="col-sm-8">
                            @Html.DisplayFor(model => model.Customer.CustomerNumber)
                        </dd>
                        <dt class="col-sm-4">
                            @Html.DisplayNameFor(model => model.Customer.Name)
                        </dt>
                        <dd class="col-sm-8">
                            @Html.DisplayFor(model => model.Customer.Name)
                        </dd>
                        <dt class="col-sm-4">
                            @Html.DisplayNameFor(model => model.Customer.Type)
                        </dt>
                        <dd class="col-sm-8">
                            @Html.DisplayFor(model => model.Customer.Type)
                        </dd>
                        <dt class="col-sm-4">
                            @Html.DisplayNameFor(model => model.Customer.CreditRating)
                        </dt>
                        <dd class="col-sm-8">
                            @Html.DisplayFor(model => model.Customer.CreditRating)
                        </dd>
                        <dt class="col-sm-4">
                            @Html.DisplayNameFor(model => model.Customer.Status)
                        </dt>
                        <dd class="col-sm-8">
                            @if (Model.Customer.Status == EIMS.Models.Enums.CustomerStatus.Active)
                            {
                                <span class="badge bg-success">@Html.DisplayFor(model => model.Customer.Status)</span>
                            }
                            else if (Model.Customer.Status == EIMS.Models.Enums.CustomerStatus.Potential)
                            {
                                <span class="badge bg-info">@Html.DisplayFor(model => model.Customer.Status)</span>
                            }
                            else if (Model.Customer.Status == EIMS.Models.Enums.CustomerStatus.Inactive)
                            {
                                <span class="badge bg-warning">@Html.DisplayFor(model => model.Customer.Status)</span>
                            }
                            else
                            {
                                <span class="badge bg-danger">@Html.DisplayFor(model => model.Customer.Status)</span>
                            }
                        </dd>
                    </dl>
                </div>
                <div class="col-md-6">
                    <dl class="row">
                        <dt class="col-sm-4">
                            @Html.DisplayNameFor(model => model.Customer.ContactPerson)
                        </dt>
                        <dd class="col-sm-8">
                            @Html.DisplayFor(model => model.Customer.ContactPerson)
                        </dd>
                        <dt class="col-sm-4">
                            @Html.DisplayNameFor(model => model.Customer.PhoneNumber)
                        </dt>
                        <dd class="col-sm-8">
                            @Html.DisplayFor(model => model.Customer.PhoneNumber)
                        </dd>
                        <dt class="col-sm-4">
                            @Html.DisplayNameFor(model => model.Customer.Email)
                        </dt>
                        <dd class="col-sm-8">
                            @Html.DisplayFor(model => model.Customer.Email)
                        </dd>
                        <dt class="col-sm-4">
                            @Html.DisplayNameFor(model => model.Customer.Fax)
                        </dt>
                        <dd class="col-sm-8">
                            @Html.DisplayFor(model => model.Customer.Fax)
                        </dd>
                        <dt class="col-sm-4">
                            @Html.DisplayNameFor(model => model.Customer.Website)
                        </dt>
                        <dd class="col-sm-8">
                            @if (!string.IsNullOrEmpty(Model.Customer.Website))
                            {
                                <a href="@Model.Customer.Website" target="_blank">@Model.Customer.Website</a>
                            }
                        </dd>
                    </dl>
                </div>
            </div>
            
            <div class="row mt-3">
                <div class="col-md-12">
                    <dl class="row">
                        <dt class="col-sm-2">
                            @Html.DisplayNameFor(model => model.Customer.Address)
                        </dt>
                        <dd class="col-sm-10">
                            @Html.DisplayFor(model => model.Customer.Address)
                        </dd>
                        <dt class="col-sm-2">
                            @Html.DisplayNameFor(model => model.Customer.Remarks)
                        </dt>
                        <dd class="col-sm-10">
                            @Html.DisplayFor(model => model.Customer.Remarks)
                        </dd>
                        <dt class="col-sm-2">
                            @Html.DisplayNameFor(model => model.Customer.CreatedAt)
                        </dt>
                        <dd class="col-sm-10">
                            @Model.Customer.CreatedAt.ToString("yyyy-MM-dd HH:mm:ss")
                        </dd>
                        <dt class="col-sm-2">
                            @Html.DisplayNameFor(model => model.Customer.UpdatedAt)
                        </dt>
                        <dd class="col-sm-10">
                            @(Model.Customer.UpdatedAt.HasValue ? Model.Customer.UpdatedAt.Value.ToString("yyyy-MM-dd HH:mm:ss") : "-")
                        </dd>
                    </dl>
                </div>
            </div>
        </div>
        <div class="card-footer">
            <div class="d-flex">
                <a asp-action="Index" class="btn btn-secondary me-2">
                    <i class="fas fa-arrow-left"></i> 返回列表
                </a>
                @if (User.IsInRole("Administrator") || User.IsInRole("Manager"))
                {
                    <a asp-action="Edit" asp-route-id="@Model.Customer.Id" class="btn btn-primary me-2">
                        <i class="fas fa-edit"></i> 编辑
                    </a>
                }
            </div>
        </div>
    </div>
    
    <!-- 客户联系人信息 -->
    <div class="card mb-4">
        <div class="card-header">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <i class="fas fa-address-book me-1"></i>
                    联系人信息
                </div>
                <div>
                    @if (User.IsInRole("Administrator") || User.IsInRole("Manager"))
                    {
                        <a asp-action="AddContact" asp-route-id="@Model.Customer.Id" class="btn btn-primary btn-sm">
                            <i class="fas fa-plus"></i> 添加联系人
                        </a>
                    }
                </div>
            </div>
        </div>
        <div class="card-body">
            @if (Model.Contacts != null && Model.Contacts.Any())
            {
                <div class="table-responsive">
                    <table class="table table-striped table-hover">
                        <thead>
                            <tr>
                                <th>姓名</th>
                                <th>职位</th>
                                <th>部门</th>
                                <th>手机</th>
                                <th>电子邮箱</th>
                                <th>是否主要联系人</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var contact in Model.Contacts)
                            {
                                <tr>
                                    <td>@contact.Name</td>
                                    <td>@contact.Position</td>
                                    <td>@contact.Department</td>
                                    <td>@contact.Mobile</td>
                                    <td>@contact.Email</td>
                                    <td>
                                        @if (contact.IsPrimary)
                                        {
                                            <span class="badge bg-success">是</span>
                                        }
                                        else
                                        {
                                            <span>否</span>
                                        }
                                    </td>
                                    <td>
                                        <div class="btn-group">
                                            @if (User.IsInRole("Administrator") || User.IsInRole("Manager"))
                                            {
                                                <a asp-action="EditContact" asp-route-id="@contact.Id" class="btn btn-primary btn-sm" title="编辑">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                <a asp-action="DeleteContact" asp-route-id="@contact.Id" class="btn btn-danger btn-sm" title="删除">
                                                    <i class="fas fa-trash"></i>
                                                </a>
                                            }
                                        </div>
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            }
            else
            {
                <div class="alert alert-info">
                    暂无联系人信息
                </div>
            }
        </div>
    </div>
    
    <!-- 客户交易记录 -->
    <div class="card mb-4">
        <div class="card-header">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <i class="fas fa-money-check-alt me-1"></i>
                    交易记录
                </div>
                <div>
                    @if (User.IsInRole("Administrator") || User.IsInRole("Manager"))
                    {
                        <a asp-action="AddTransaction" asp-route-id="@Model.Customer.Id" class="btn btn-primary btn-sm">
                            <i class="fas fa-plus"></i> 添加交易
                        </a>
                    }
                </div>
            </div>
        </div>
        <div class="card-body">
            @if (Model.TransactionCount > 0)
            {
                <div class="row mb-3">
                    <div class="col-md-4">
                        <div class="card bg-primary text-white">
                            <div class="card-body">
                                <h5 class="card-title">总交易金额</h5>
                                <p class="card-text h4">¥ @Model.TotalTransactionAmount.ToString("N2")</p>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="card bg-warning text-white">
                            <div class="card-body">
                                <h5 class="card-title">未结算金额</h5>
                                <p class="card-text h4">¥ @Model.UnpaidAmount.ToString("N2")</p>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="card bg-info text-white">
                            <div class="card-body">
                                <h5 class="card-title">交易次数</h5>
                                <p class="card-text h4">@Model.TransactionCount 次</p>
                            </div>
                        </div>
                    </div>
                </div>
            
                <div class="table-responsive">
                    <table class="table table-striped table-hover">
                        <thead>
                            <tr>
                                <th>交易编号</th>
                                <th>交易类型</th>
                                <th>交易日期</th>
                                <th>金额</th>
                                <th>付款状态</th>
                                <th>付款日期</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var transaction in Model.Transactions)
                            {
                                <tr>
                                    <td>@transaction.TransactionNumber</td>
                                    <td>@transaction.Type</td>
                                    <td>@transaction.TransactionDate.ToString("yyyy-MM-dd")</td>
                                    <td>¥ @transaction.Amount.ToString("N2")</td>
                                    <td>
                                        @if (transaction.PaymentStatus == EIMS.Models.Enums.PaymentStatus.Paid)
                                        {
                                            <span class="badge bg-success">已付款</span>
                                        }
                                        else if (transaction.PaymentStatus == EIMS.Models.Enums.PaymentStatus.PartiallyPaid)
                                        {
                                            <span class="badge bg-warning">部分付款</span>
                                        }
                                        else if (transaction.PaymentStatus == EIMS.Models.Enums.PaymentStatus.Unpaid)
                                        {
                                            <span class="badge bg-danger">未付款</span>
                                        }
                                        else
                                        {
                                            <span class="badge bg-secondary">@transaction.PaymentStatus</span>
                                        }
                                    </td>
                                    <td>@(transaction.PaymentDate.HasValue ? transaction.PaymentDate.Value.ToString("yyyy-MM-dd") : "-")</td>
                                    <td>
                                        <div class="btn-group">
                                            @if (User.IsInRole("Administrator") || User.IsInRole("Manager"))
                                            {
                                                <a asp-action="EditTransaction" asp-route-id="@transaction.Id" class="btn btn-primary btn-sm" title="编辑">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                <a asp-action="DeleteTransaction" asp-route-id="@transaction.Id" class="btn btn-danger btn-sm" title="删除">
                                                    <i class="fas fa-trash"></i>
                                                </a>
                                            }
                                        </div>
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            }
            else
            {
                <div class="alert alert-info">
                    暂无交易记录
                </div>
            }
        </div>
    </div>
</div> 